import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Path2D;
import java.util.LinkedList;
import java.util.Random;

public class GameJFrameT extends JFrame implements ActionListener, KeyListener {
    private JPanel jPanel;
    private Timer timer;
    private boolean[] keys = new boolean[256];
    private int TX = 100;
    private int TY = 800;
    private int TX2 = 900;
    private int TY2 = 100;
    private boolean TIsLive=true;
    private boolean TIsLive2=true;
    private int pTX=100;
    private int pTY=800;
    private int pTX2=800;
    private int pTY2=100;
    private int vT;
    private int vT2;
    private double pangle = 0;
    private double pangle_2 = 0;
    private double angle;
    private double angle_2;
    private int a=40;
    private int b=46;
    private int BX1;
    private int BY1;
    private int BX2;
    private int BY2;
    private int BX3;
    private int BY3;
    private int BX4;
    private int BY4;
    private int BX5;
    private int BY5;
    private int BX1_2;
    private int BY1_2;
    private int BX2_2;
    private int BY2_2;
    private int BX3_2;
    private int BY3_2;
    private int BX4_2;
    private int BY4_2;
    private int BX5_2;
    private int BY5_2;
    private int vB=8;
    private int vBX1;
    private int vBY1;
    private int vBX2;
    private int vBY2;
    private int vBX3;
    private int vBY3;
    private int vBX4;
    private int vBY4;
    private int vBX5;
    private int vBY5;
    private int vBX1_2;
    private int vBY1_2;
    private int vBX2_2;
    private int vBY2_2;
    private int vBX3_2;
    private int vBY3_2;
    private int vBX4_2;
    private int vBY4_2;
    private int vBX5_2;
    private int vBY5_2;
    private int BLive1=0;
    private int BLive2=0;
    private int BLive3=0;
    private int BLive4=0;
    private int BLive5=0;
    private int BLive1_2=0;
    private int BLive2_2=0;
    private int BLive3_2=0;
    private int BLive4_2=0;
    private int BLive5_2=0;
    private int r=10;
    private int px1=80;
    private int py1=777;
    private int px2=120;
    private int py2=777;
    private int px3=120;
    private int py3=823;
    private int px4=80;
    private int py4=823;
    private int px1_2=80;
    private int py1_2=777;
    private int px2_2=120;
    private int py2_2=777;
    private int px3_2=120;
    private int py3_2=823;
    private int px4_2=80;
    private int py4_2=823;
    private int pangle1=49;
    private int pangle2=131;
    private int pangle3=229;
    private int pangle4=311;
    private int pangle1_2=49;
    private int pangle2_2=131;
    private int pangle3_2=229;
    private int pangle4_2=311;
    private int angle1;
    private int angle2;
    private int angle3;
    private int angle4;
    private int angle1_2;
    private int angle2_2;
    private int angle3_2;
    private int angle4_2;
    private double d=30.5;
    private boolean IsRight=false;
    private boolean IsLeft=false;
    private boolean IsGo=false;
    private boolean IsRight2=false;
    private boolean IsLeft2=false;
    private boolean IsGo2=false;
    private boolean IsB=false;
    private boolean IsB2=false;
    private boolean BIsLive1=false;
    private boolean BIsLive2=false;
    private boolean BIsLive3=false;
    private boolean BIsLive4=false;
    private boolean BIsLive5=false;
    private boolean BIsLive6=false;
    private boolean BIsLive1_2=false;
    private boolean BIsLive2_2=false;
    private boolean BIsLive3_2=false;
    private boolean BIsLive4_2=false;
    private boolean BIsLive5_2=false;
    private boolean BIsLive6_2=false;
    private int T1_type=0;
    private int T2_type=0;
    private int buff;
    private int buffTime=0;
    private int buff_X;
    private int buff_Y;
    private int buff_r=30;
    private int laserX1;
    private int laserY1;
    private int laserEndX1;
    private int laserEndY1;
    private int laserX2;
    private int laserY2;
    private int laserEndX2;
    private int laserEndY2;
    private boolean isLaserActive1=false;
    private boolean isLaserActive2=false;
    private int LaserLive1=0;
    private int LaserLive2=0;
    private int useBX1;
    private int useBY1;
    private int useBX2;
    private int useBY2;
    private int useR=35;
    private boolean isRightB1=false;
    private boolean isLeftB1=false;
    private boolean isUpB1=false;
    private boolean isDowmB1=false;
    private boolean isUseBLive1=false;
    private int useLiveTime1=0;
    private boolean isRightB2=false;
    private boolean isLeftB2=false;
    private boolean isUpB2=false;
    private boolean isDowmB2=false;
    private boolean isUseBLive2=false;
    private int useLiveTime2=0;
    private Obstacle obstacle=new Obstacle();


    public GameJFrameT() {
        initJFrame();
        timer = new Timer(15, e -> {
            if (!TIsLive){
                timer.stop();
                TIsLive=true;
                TIsLive2=true;
                vT=0;
                SonJFrame.winP=2;
                new SonJFrame().setVisible(true);
                GameJFrameT.this.dispose();
                return;
            }
            if (!TIsLive2){
                timer.stop();
                TIsLive=true;
                TIsLive2=true;
                vT=0;
                SonJFrame.winP=1;
                new SonJFrame().setVisible(true);
                GameJFrameT.this.dispose();
                return;
            }


            LaserLive1++;
            LaserLive2++;
            if(LaserLive1>=100){
                isLaserActive1=false;
                LaserLive1=0;
            }
            if(LaserLive2>=100){
                isLaserActive2=false;
                LaserLive2=0;
            }


            useLiveTime1++;
            useLiveTime2++;
            if(useLiveTime1>=600){
                useLiveTime1=0;
                isUseBLive1=false;
                useBX1=-20;
                isRightB1=false;
                isLeftB1=false;
                isUpB1=false;
                isDowmB1=false;
            }
            if(useLiveTime2>=600){
                useLiveTime2=0;
                isUseBLive2=false;
                useBX2=-20;
                isRightB2=false;
                isLeftB2=false;
                isUpB2=false;
                isDowmB2=false;
            }
            if(isUseBLive1){
                if(isRightB1){
                    useBX1+=3;
                }
                if(isLeftB1){
                    useBX1-=3;
                }
                if(isUpB1){
                    useBY1-=3;
                }
                if(isDowmB1){
                    useBY1+=3;
                }
            }
            if(isUseBLive2){
                if(isRightB2){
                    useBX2+=3;
                }
                if(isLeftB2){
                    useBX2-=3;
                }
                if(isUpB2){
                    useBY2-=3;
                }
                if(isDowmB2){
                    useBY2+=3;
                }
            }

            //生成buff
            buffTime++;
            if(buffTime>=200 && buff==0){
                buffTime=0;
                createBuff();
            }
            T1eatBuff();
            T2eatBuff();
            //1号坦克
            changeDot();
            if(IsGo){
                Go();
                if(!checkObstacle()){
                    TX=pTX;
                    TY=pTY;
                }else {
                    pTX=TX;
                    pTY=TY;
                }
            }
            if(IsRight){
                Right();
                if(!checkObstacle()){
                    angle=pangle;
                    angle1=pangle1;
                    angle2=pangle2;
                    angle3=pangle3;
                    angle4=pangle4;
                }else {
                    pangle=angle;
                    pangle1=angle1;
                    pangle2=angle2;
                    pangle3=angle3;
                    pangle4=angle4;
                }
            }
            if(IsLeft){
                Left();
                if(!checkObstacle()){
                    angle=pangle;
                    angle1=pangle1;
                    angle2=pangle2;
                    angle3=pangle3;
                    angle4=pangle4;
                }else {
                    pangle=angle;
                    pangle1=angle1;
                    pangle2=angle2;
                    pangle3=angle3;
                    pangle4=angle4;
                }
            }
            if(IsB){
                createB();
            }

            if(BLive1==750){
                BLive1=0;
                BIsLive1=false;
                BX1=-10;
                vBX1=0;
            }
            if(BLive2==750){
                BLive2=0;
                BIsLive2=false;
                BX2=-10;
                vBX2=0;
            }
            if(BLive3==750){
                BLive3=0;
                BIsLive3=false;
                BX3=-10;
                vBX3=0;
            }
            if(BLive4==750){
                BLive4=0;
                BIsLive4=false;
                BX4=-10;
                vBX4=0;
            }
            if(BLive5==750){
                BLive5=0;
                BIsLive5=false;
                BX5=-10;
                vBX5=0;
            }

            if (BIsLive1) {
                BLive1++;
                int steps = calculateMovementSteps(vBX1,vBY1);
                for (int i = 0; i < steps; i++) {
                    BX1 -= vBX1 / steps;
                    BY1 -= vBY1 / steps;
                    if (checkBulletCollision1(BX1,BY1)) break;
                }
            }
            if (BIsLive2) {
                BLive2++;
                int steps = calculateMovementSteps(vBX2,vBY2);
                for (int i = 0; i < steps; i++) {
                    BX2 -= vBX2 / steps;
                    BY2 -= vBY2 / steps;
                    if (checkBulletCollision2(BX2,BY2)) break;
                }
            }
            if (BIsLive3) {
                BLive3++;
                int steps = calculateMovementSteps(vBX3,vBY3);
                for (int i = 0; i < steps; i++) {
                    BX3 -= vBX3 / steps;
                    BY3 -= vBY3 / steps;
                    if (checkBulletCollision3(BX3,BY3)) break;
                }
            }
            if (BIsLive4) {
                BLive4++;
                int steps = calculateMovementSteps(vBX4,vBY4);
                for (int i = 0; i < steps; i++) {
                    BX4 -= vBX4 / steps;
                    BY4 -= vBY4 / steps;
                    if (checkBulletCollision4(BX4,BY4)) break;
                }
            }
            if (BIsLive5) {
                BLive5++;
                int steps = calculateMovementSteps(vBX5,vBY5);
                for (int i = 0; i < steps; i++) {
                    BX5 -= vBX5 / steps;
                    BY5 -= vBY5 / steps;
                    if (checkBulletCollision5(BX5,BY5)) break;
                }
            }

            checkBHitT1();
            checkBHitT2();
            checkBHitT3();
            checkBHitT4();
            checkBHitT5();
            checkBHitT6();
            checkBHitT7();
            checkBHitT8();
            checkBHitT9();
            checkBHitT10();
            checkBHitT11();

            //2号坦克
            changeDot2();
            if(IsGo2){
                Go2();
                if(!checkObstacle2()){
                    TX2=pTX2;
                    TY2=pTY2;
                }else {
                    pTX2=TX2;
                    pTY2=TY2;
                }
            }
            if(IsRight2){
                Right2();
                if(!checkObstacle2()){
                    angle_2=pangle_2;
                    angle1_2=pangle1_2;
                    angle2_2=pangle2_2;
                    angle3_2=pangle3_2;
                    angle4_2=pangle4_2;
                }else {
                    pangle_2=angle_2;
                    pangle1_2=angle1_2;
                    pangle2_2=angle2_2;
                    pangle3_2=angle3_2;
                    pangle4_2=angle4_2;
                }
            }
            if(IsLeft2){
                Left2();
                if(!checkObstacle2()){
                    angle_2=pangle_2;
                    angle1_2=pangle1_2;
                    angle2_2=pangle2_2;
                    angle3_2=pangle3_2;
                    angle4_2=pangle4_2;
                }else {
                    pangle_2=angle_2;
                    pangle1_2=angle1_2;
                    pangle2_2=angle2_2;
                    pangle3_2=angle3_2;
                    pangle4_2=angle4_2;
                }
            }
            if(IsB2){
                createB2();
            }

            if(BLive1_2==750){
                BLive1_2=0;
                BIsLive1_2=false;
                BX1_2=-10;
                vBX1_2=0;
            }
            if(BLive2_2==750){
                BLive2_2=0;
                BIsLive2_2=false;
                BX2_2=-10;
                vBX2_2=0;
            }
            if(BLive3_2==750){
                BLive3_2=0;
                BIsLive3_2=false;
                BX3_2=-10;
                vBX3_2=0;
            }
            if(BLive4_2==750){
                BLive4_2=0;
                BIsLive4_2=false;
                BX4_2=-10;
                vBX4_2=0;
            }
            if(BLive5_2==750){
                BLive5_2=0;
                BIsLive5_2=false;
                BX5_2=-10;
                vBX5_2=0;
            }

            if (BIsLive1_2) {
                BLive1_2++;
                int steps = calculateMovementSteps(vBX1_2,vBY1_2);
                for (int i = 0; i < steps; i++) {
                    BX1_2 -= vBX1_2 / steps;
                    BY1_2 -= vBY1_2 / steps;
                    if (checkBulletCollision1_2(BX1_2,BY1_2)) break;
                }
            }
            if (BIsLive2_2) {
                BLive2_2++;
                int steps = calculateMovementSteps(vBX2_2,vBY2_2);
                for (int i = 0; i < steps; i++) {
                    BX2_2 -= vBX2_2 / steps;
                    BY2_2 -= vBY2_2 / steps;
                    if (checkBulletCollision2_2(BX2_2,BY2_2)) break;
                }
            }
            if (BIsLive3_2) {
                BLive3_2++;
                int steps = calculateMovementSteps(vBX3_2,vBY3_2);
                for (int i = 0; i < steps; i++) {
                    BX3_2 -= vBX3_2 / steps;
                    BY3_2 -= vBY3_2 / steps;
                    if (checkBulletCollision3_2(BX3_2,BY3_2)) break;
                }
            }
            if (BIsLive4_2) {
                BLive4_2++;
                int steps = calculateMovementSteps(vBX4_2,vBY4_2);
                for (int i = 0; i < steps; i++) {
                    BX4_2 -= vBX4_2 / steps;
                    BY4_2 -= vBY4_2 / steps;
                    if (checkBulletCollision4_2(BX4_2,BY4_2)) break;
                }
            }
            if (BIsLive5_2) {
                BLive5_2++;
                int steps = calculateMovementSteps(vBX5_2,vBY5_2);
                for (int i = 0; i < steps; i++) {
                    BX5_2 -= vBX5_2 / steps;
                    BY5_2 -= vBY5_2 / steps;
                    if (checkBulletCollision5_2(BX5_2,BY5_2)) break;
                }
            }

            checkBHitT1_2();
            checkBHitT2_2();
            checkBHitT3_2();
            checkBHitT4_2();
            checkBHitT5_2();
            checkBHitT6_2();
            checkBHitT7_2();
            checkBHitT8_2();
            checkBHitT9_2();
            checkBHitT10_2();
            checkBHitT11_2();


            repaint();
        });
        timer.start();
        initGamePanel();
        setVisible(true);
    }

    private void initJFrame() {
        setTitle("自律的男生制作的坦克大战");
        setSize(1000, 1000);
        setLayout(null);
        setAlwaysOnTop(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        requestFocusInWindow();
        addKeyListener(this);
    }

    private void initGamePanel() {
        jPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                //转换为Graphics2D
                Graphics2D g2d = (Graphics2D) g.create();
                // 设置抗锯齿
                g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                //给背景涂色
                g2d.setColor(new Color(229, 229, 229));
                g2d.fillRect(0, 0, 1000, 1000);
                //绘制墙体
                g2d.setColor(Color.DARK_GRAY);
                LinkedList<Ob> obs=obstacle.getObs();
                for(Ob ob:obs){
                    g2d.fillRect(ob.getX(),ob.getY(),ob.getWidth(),ob.getHeight());
                }
                //绘制炮弹(写在变换前)
                g2d.setColor(new Color(20,20,20));
                if(BIsLive1){
                    g2d.fillOval(BX1-r/2,BY1-r/2,r,r);
                }
                if(BIsLive2){
                    g2d.fillOval(BX2-r/2,BY2-r/2,r,r);
                }
                if(BIsLive3){
                    g2d.fillOval(BX3-r/2,BY3-r/2,r,r);
                }
                if(BIsLive4){
                    g2d.fillOval(BX4-r/2,BY4-r/2,r,r);
                }
                if(BIsLive5){
                    g2d.fillOval(BX5-r/2,BY5-r/2,r,r);
                }
                if(BIsLive1_2){
                    g2d.fillOval(BX1_2-r/2,BY1_2-r/2,r,r);
                }
                if(BIsLive2_2){
                    g2d.fillOval(BX2_2-r/2,BY2_2-r/2,r,r);
                }
                if(BIsLive3_2){
                    g2d.fillOval(BX3_2-r/2,BY3_2-r/2,r,r);
                }
                if(BIsLive4_2){
                    g2d.fillOval(BX4_2-r/2,BY4_2-r/2,r,r);
                }
                if(BIsLive5_2){
                    g2d.fillOval(BX5_2-r/2,BY5_2-r/2,r,r);
                }
                //绘制buff
                if(buff==1){
                    g2d.setColor(Color.CYAN);
                    g2d.fillOval(buff_X-buff_r/2,buff_Y-buff_r/2,buff_r,buff_r);
                }else if(buff==2){
                    g2d.setColor(Color.ORANGE);
                    g2d.fillOval(buff_X-buff_r/2,buff_Y-buff_r/2,buff_r,buff_r);
                }
                //绘制激光
                if (isLaserActive1) {
                    g2d.setColor(Color.CYAN);
                    g2d.setStroke(new BasicStroke(a/2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                    g2d.drawLine(laserX1, laserY1, laserEndX1, laserEndY1);
                }

                if (isLaserActive2) {
                    g2d.setColor(Color.CYAN);
                    g2d.setStroke(new BasicStroke(a/2, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
                    g2d.drawLine(laserX2, laserY2, laserEndX2, laserEndY2);

                }
                //绘制无人机炸弹
                if(isUseBLive1){
                    g2d.setColor(Color.ORANGE);
                    g2d.fillOval(useBX1-useR/2,useBY1-useR/2,useR,useR);
                }
                if(isUseBLive2){
                    g2d.setColor(Color.ORANGE);
                    g2d.fillOval(useBX2-useR/2,useBY2-useR/2,useR,useR);
                }

                // 应用旋转变换
                AffineTransform old = g2d.getTransform();
                g2d.rotate(Math.toRadians(angle), TX, TY);
                //绘制坦克
                if(TIsLive){
                    g2d.setColor(Color.RED);
                    g2d.fillRect(TX-a/2,TY-b/2,a,b);
                    g2d.setColor(new Color(220,0,0));
                    g2d.fillRect(TX-a/4,TY-a/4,a/2,a/2);
                    g2d.fillRect(TX-a/8,TY-33,a/4,33);
                    if(T1_type==1){
                        g2d.setColor(Color.CYAN);
                        g2d.fillOval(TX-a/4,TY-a/4,a/2,a/2);
                    } else if (T1_type==2) {
                        g2d.setColor(Color.ORANGE);
                        g2d.fillOval(TX-a/4,TY-a/4,a/2,a/2);
                    }
                }
                // 绘制爆炸动画
                if (!TIsLive) {
                    g2d.setColor(Color.RED);
                    for(int i=-25;i<=25;i+=10){
                        for(int j=-25;j<=25;j+=10){
                            g2d.fillRect(TX-i,TY-j,5,5);
                        }
                    }
                }
                //恢复初始坐标系
                g2d.setTransform(old);

                // 应用旋转变换
                AffineTransform old2 = g2d.getTransform();
                g2d.rotate(Math.toRadians(angle_2), TX2, TY2);
                if(TIsLive2){
                    g2d.setColor(Color.BLUE);
                    g2d.fillRect(TX2-a/2,TY2-b/2,a,b);
                    g2d.setColor(new Color(0,0,220));
                    g2d.fillRect(TX2-a/4,TY2-a/4,a/2,a/2);
                    g2d.fillRect(TX2-a/8,TY2-33,a/4,33);
                    if(T2_type==1){
                        g2d.setColor(Color.CYAN);
                        g2d.fillOval(TX2-a/4,TY2-a/4,a/2,a/2);
                    } else if (T2_type==2) {
                        g2d.setColor(Color.ORANGE);
                        g2d.fillOval(TX2-a/4,TY2-a/4,a/2,a/2);
                    }
                }
                // 绘制爆炸动画
                if(!TIsLive2){
                    g2d.setColor(Color.BLUE);
                    for(int i=-25;i<=25;i+=10){
                        for(int j=-25;j<=25;j+=10){
                            g2d.fillRect(TX2-i,TY2-j,5,5);
                        }
                    }
                }
                //恢复初始坐标系
                g2d.setTransform(old2);
            }
        };
        jPanel.setBounds(0, 0, 1000, 1000);
        add(jPanel);
    }

    private void Go(){
        pTX-= (int) (vT*Math.cos(Math.toRadians(angle+90)));
        pTY-= (int) (vT*Math.sin(Math.toRadians(angle+90)));
    }

    private void Go2(){
        pTX2-= (int) (vT2*Math.cos(Math.toRadians(angle_2+90)));
        pTY2-= (int) (vT2*Math.sin(Math.toRadians(angle_2+90)));
    }

    private void Right(){
        if(pangle>=360){
            pangle-=360;
        } else if (pangle<0) {
            pangle+=360;
        }
        pangle += 5;
        pangle1 += 5;
        pangle2 += 5;
        pangle3 += 5;
        pangle4 += 5;
    }

    private void Right2(){
        if(pangle_2>=360){
            pangle_2-=360;
        } else if (pangle_2<0) {
            pangle_2+=360;
        }
        pangle_2 += 5;
        pangle1_2 += 5;
        pangle2_2 += 5;
        pangle3_2 += 5;
        pangle4_2 += 5;
    }

    private void Left(){
        if(pangle>=360){
            pangle-=360;
        } else if (pangle<0) {
            pangle+=360;
        }
        pangle -= 5;
        pangle1 -= 5;
        pangle2 -= 5;
        pangle3 -= 5;
        pangle4 -= 5;
    }

    private void Left2(){
        if(pangle_2>=360){
            pangle_2-=360;
        } else if (pangle_2<0) {
            pangle_2+=360;
        }
        pangle_2 -= 5;
        pangle1_2 -= 5;
        pangle2_2 -= 5;
        pangle3_2 -= 5;
        pangle4_2 -= 5;
    }

    private void createB(){
        IsB=false;
        if(T1_type==1){
            isLaserActive1=true;
            calculateLaserPath1();
            checkLaserCollision1();
            T1_type=0;
            return;
        } else if (T1_type==2) {
            isUseBLive1=true;
            useBX1=TX-(int) ((b/2+20)*Math.cos(Math.toRadians(angle+90)));
            useBY1=TY-(int) ((b/2+20)*Math.sin(Math.toRadians(angle+90)));
            T1_type=0;
            return;
        }
        if(BLive1==0){
            BX1=TX-(int) ((b/2+10)*Math.cos(Math.toRadians(angle+90)));
            BY1=TY-(int) ((b/2+10)*Math.sin(Math.toRadians(angle+90)));
            vBX1= (int) (vB*Math.cos(Math.toRadians(angle+90)));
            vBY1= (int) (vB*Math.sin(Math.toRadians(angle+90)));
            BLive1=1;
            BIsLive1=true;
        } else if (BLive1!=0 && BLive2==0) {
            BX2=TX-(int) ((b/2+10)*Math.cos(Math.toRadians(angle+90)));
            BY2=TY-(int) ((b/2+10)*Math.sin(Math.toRadians(angle+90)));
            vBX2= (int) (vB*Math.cos(Math.toRadians(angle+90)));
            vBY2= (int) (vB*Math.sin(Math.toRadians(angle+90)));
            BLive2=1;
            BIsLive2=true;
        } else if (BLive1!=0 && BLive2!=0 && BLive3==0) {
            BX3=TX-(int) ((b/2+10)*Math.cos(Math.toRadians(angle+90)));
            BY3=TY-(int) ((b/2+10)*Math.sin(Math.toRadians(angle+90)));
            vBX3= (int) (vB*Math.cos(Math.toRadians(angle+90)));
            vBY3= (int) (vB*Math.sin(Math.toRadians(angle+90)));
            BLive3=1;
            BIsLive3=true;
        } else if (BLive1!=0 && BLive2!=0 && BLive3!=0 && BLive4==0) {
            BX4=TX-(int) ((b/2+10)*Math.cos(Math.toRadians(angle+90)));
            BY4=TY-(int) ((b/2+10)*Math.sin(Math.toRadians(angle+90)));
            vBX4= (int) (vB*Math.cos(Math.toRadians(angle+90)));
            vBY4= (int) (vB*Math.sin(Math.toRadians(angle+90)));
            BLive4=1;
            BIsLive4=true;
        } else if (BLive1!=0 && BLive2!=0 && BLive3!=0 && BLive4!=0 && BLive5==0) {
            BX5=TX-(int) ((b/2+10)*Math.cos(Math.toRadians(angle+90)));
            BY5=TY-(int) ((b/2+10)*Math.sin(Math.toRadians(angle+90)));
            vBX5= (int) (vB*Math.cos(Math.toRadians(angle+90)));
            vBY5= (int) (vB*Math.sin(Math.toRadians(angle+90)));
            BLive5=1;
            BIsLive5=true;
        }
    }

    private void createB2(){
        IsB2=false;
        if(T2_type==1){
            isLaserActive2=true;
            calculateLaserPath2();
            checkLaserCollision2();
            T2_type=0;
            return;
        } else if (T2_type==2) {
            isUseBLive2=true;
            useBX2=TX2-(int) ((b/2+20)*Math.cos(Math.toRadians(angle_2+90)));
            useBY2=TY2-(int) ((b/2+20)*Math.sin(Math.toRadians(angle_2+90)));
            T2_type=0;
            return;
        }
        if(BLive1_2==0){
            BX1_2=TX2-(int) ((b/2+10)*Math.cos(Math.toRadians(angle_2+90)));
            BY1_2=TY2-(int) ((b/2+10)*Math.sin(Math.toRadians(angle_2+90)));
            vBX1_2= (int) (vB*Math.cos(Math.toRadians(angle_2+90)));
            vBY1_2= (int) (vB*Math.sin(Math.toRadians(angle_2+90)));
            BLive1_2=1;
            BIsLive1_2=true;
        } else if (BLive1_2!=0 && BLive2_2==0) {
            BX2_2=TX2-(int) ((b/2+10)*Math.cos(Math.toRadians(angle_2+90)));
            BY2_2=TY2-(int) ((b/2+10)*Math.sin(Math.toRadians(angle_2+90)));
            vBX2_2= (int) (vB*Math.cos(Math.toRadians(angle_2+90)));
            vBY2_2= (int) (vB*Math.sin(Math.toRadians(angle_2+90)));
            BLive2_2=1;
            BIsLive2_2=true;
        } else if (BLive1_2!=0 && BLive2_2!=0 && BLive3_2==0) {
            BX3_2=TX2-(int) ((b/2+10)*Math.cos(Math.toRadians(angle_2+90)));
            BY3_2=TY2-(int) ((b/2+10)*Math.sin(Math.toRadians(angle_2+90)));
            vBX3_2= (int) (vB*Math.cos(Math.toRadians(angle_2+90)));
            vBY3_2= (int) (vB*Math.sin(Math.toRadians(angle_2+90)));
            BLive3_2=1;
            BIsLive3_2=true;
        } else if (BLive1_2!=0 && BLive2_2!=0 && BLive3_2!=0 && BLive4_2==0) {
            BX4_2=TX2-(int) ((b/2+10)*Math.cos(Math.toRadians(angle_2+90)));
            BY4_2=TY2-(int) ((b/2+10)*Math.sin(Math.toRadians(angle_2+90)));
            vBX4_2= (int) (vB*Math.cos(Math.toRadians(angle_2+90)));
            vBY4_2= (int) (vB*Math.sin(Math.toRadians(angle_2+90)));
            BLive4_2=1;
            BIsLive4_2=true;
        } else if (BLive1_2!=0 && BLive2_2!=0 && BLive3_2!=0 && BLive4_2!=0 && BLive5_2==0) {
            BX5_2=TX2-(int) ((b/2+10)*Math.cos(Math.toRadians(angle_2+90)));
            BY5_2=TY2-(int) ((b/2+10)*Math.sin(Math.toRadians(angle_2+90)));
            vBX5_2= (int) (vB*Math.cos(Math.toRadians(angle_2+90)));
            vBY5_2= (int) (vB*Math.sin(Math.toRadians(angle_2+90)));
            BLive5_2=1;
            BIsLive5_2=true;
        }
    }

    private void changeDot(){
        pangle1%=360;
        pangle2%=360;
        pangle3%=360;
        pangle4%=360;
//        px1=(int) (TX+d*Math.cos(Math.toRadians(pangle1)));
//        py1=(int) (TY+d*Math.sin(Math.toRadians(pangle1)));
//        px2=(int) (TX+d*Math.cos(Math.toRadians(pangle2)));
//        py2=(int) (TY+d*Math.sin(Math.toRadians(pangle2)));
//        px3=(int) (TX+d*Math.cos(Math.toRadians(pangle3)));
//        py3=(int) (TY+d*Math.sin(Math.toRadians(pangle3)));
//        px4=(int) (TX+d*Math.cos(Math.toRadians(pangle4)));
//        py4=(int) (TY+d*Math.sin(Math.toRadians(pangle4)));
    }

    private void changeDot2(){
        pangle1_2%=360;
        pangle2_2%=360;
        pangle3_2%=360;
        pangle4_2%=360;
//        px1_2=(int) (TX2+d*Math.cos(Math.toRadians(pangle1_2)));
//        py1_2=(int) (TY2+d*Math.sin(Math.toRadians(pangle1_2)));
//        px2_2=(int) (TX2+d*Math.cos(Math.toRadians(pangle2_2)));
//        py2_2=(int) (TY2+d*Math.sin(Math.toRadians(pangle2_2)));
//        px3_2=(int) (TX2+d*Math.cos(Math.toRadians(pangle3_2)));
//        py3_2=(int) (TY2+d*Math.sin(Math.toRadians(pangle3_2)));
//        px4_2=(int) (TX2+d*Math.cos(Math.toRadians(pangle4_2)));
//        py4_2=(int) (TY2+d*Math.sin(Math.toRadians(pangle4_2)));
    }

    private boolean checkObstacle(){
//        LinkedList<Ob> obs=obstacle.getObs();
//        int h=0;
//        for(Ob ob:obs){
//            Rectangle obF=new Rectangle(ob.getX(),ob.getY(),ob.getWidth(),ob.getHeight());
//            for(int i=0;i<4;i++){
//                Rectangle dotF=new Rectangle(dot[i][0],dot[i][1],5,5);
//                if(dotF.intersects(obF)){
//                    h++;
//                    break;
//                }
//            }
//        }
//        if(h!=0){
//            return true;
//        }
//        return false;

        Rectangle tankRect = new Rectangle(pTX - a/2, pTY - b/2, a, b);
        for (Ob ob : obstacle.getObs()) {
            Rectangle obRect = new Rectangle(ob.getX(), ob.getY(), ob.getWidth(), ob.getHeight());

            if (rotatedRectIntersectsObstacle(tankRect, obRect, pangle)) {
                return true;
            }
        }
        return false;
    }

    private boolean checkObstacle2(){
        Rectangle tankRect = new Rectangle(pTX2 - a/2, pTY2 - b/2, a, b);
        for (Ob ob : obstacle.getObs()) {
            Rectangle obRect = new Rectangle(ob.getX(), ob.getY(), ob.getWidth(), ob.getHeight());

            if (rotatedRectIntersectsObstacle2(tankRect, obRect, pangle_2)) {
                return true;
            }
        }
        return false;
    }

    private boolean rotatedRectIntersectsObstacle(Rectangle rect, Rectangle obstacle, double pangle) {
        Area rotatedTank = new Area(rect);
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(pangle), pTX, pTY);
        rotatedTank.transform(transform);

        Area obArea = new Area(obstacle);
        obArea.intersect(rotatedTank);
        return !obArea.isEmpty();
    }

    private boolean rotatedRectIntersectsObstacle2(Rectangle rect, Rectangle obstacle, double pangle_2) {
        Area rotatedTank = new Area(rect);
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(pangle_2), pTX2, pTY2);
        rotatedTank.transform(transform);

        Area obArea = new Area(obstacle);
        obArea.intersect(rotatedTank);
        return !obArea.isEmpty();
    }

    private boolean checkBulletCollision1(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY1 = -vBY1;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX1 = -vBX1;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY2 = -vBY2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX2 = -vBX2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision3(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY3 = -vBY3;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX3 = -vBX3;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision4(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY4 = -vBY4;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX4 = -vBX4;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision5(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY5 = -vBY5;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX5 = -vBX5;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }


    private boolean checkBulletCollision1_2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY1_2 = -vBY1_2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX1_2 = -vBX1_2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision2_2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY2_2 = -vBY2_2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX2_2 = -vBX2_2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision3_2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY3_2 = -vBY3_2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX3_2 = -vBX3_2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision4_2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY4_2 = -vBY4_2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX4_2 = -vBX4_2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }

    private boolean checkBulletCollision5_2(int BX,int BY) {
        boolean collided = false;
        for (Ob ob : obstacle.getObs()) {

            int obLeft = ob.getX();
            int obRight = ob.getX() + ob.getWidth();
            int obTop = ob.getY();
            int obBottom = ob.getY() + ob.getHeight();

            int closestX = Math.max(obLeft, Math.min(BX, obRight));
            int closestY = Math.max(obTop, Math.min(BY, obBottom));

            double dx = BX - closestX;
            double dy = BY - closestY;
            double distanceSq = dx * dx + dy * dy;

            if (distanceSq < r * r) {
                boolean verticalCollision = (Math.abs(dy) > Math.abs(dx));

                if (verticalCollision) {
                    vBY5_2 = -vBY5_2;
                    BY = (BY < obTop) ? obTop - r : obBottom + r;
                } else {
                    vBX5_2 = -vBX5_2;
                    BX = (BX < obLeft) ? obLeft - r : obRight + r;
                }

                collided = true;
                break;
            }
        }
        return collided;
    }


    private int calculateMovementSteps(int vBX,int vBY) {
        double distance = Math.sqrt(vBX * vBX + vBY * vBY);
        return Math.max(1, (int) Math.ceil(distance / r));
    }


    private void checkBHitT1() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX1-TX;
        double dy=BY1-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive1 = false;
        }
    }

    private void checkBHitT1_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX1_2-TX2;
        double dy=BY1_2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive1_2 = false;
        }
    }

    private void checkBHitT2() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX2-TX;
        double dy=BY2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive2 = false;
        }
    }

    private void checkBHitT2_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX2_2-TX2;
        double dy=BY2_2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive2_2 = false;
        }
    }

    private void checkBHitT3() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX3-TX;
        double dy=BY3-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive3 = false;
        }
    }

    private void checkBHitT3_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX3_2-TX2;
        double dy=BY3_2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive3_2 = false;
        }
    }

    private void checkBHitT4() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX4-TX;
        double dy=BY4-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive4 = false;
        }
    }

    private void checkBHitT4_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX4_2-TX2;
        double dy=BY4_2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive4_2 = false;
        }
    }

    private void checkBHitT5() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX5-TX;
        double dy=BY5-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive5 = false;
        }
    }

    private void checkBHitT5_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX5_2-TX2;
        double dy=BY5_2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive5_2 = false;
        }
    }

    private void checkBHitT6() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX1_2-TX;
        double dy=BY1_2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive1_2 = false;
        }
    }

    private void checkBHitT6_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX1-TX2;
        double dy=BY1-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive1 = false;
        }
    }

    private void checkBHitT7() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX2_2-TX;
        double dy=BY2_2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive2_2 = false;
        }
    }

    private void checkBHitT7_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX2-TX2;
        double dy=BY2-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive2 = false;
        }
    }

    private void checkBHitT8() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX3_2-TX;
        double dy=BY3_2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive3_2 = false;
        }
    }

    private void checkBHitT8_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX3-TX2;
        double dy=BY3-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive3 = false;
        }
    }

    private void checkBHitT9() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX4_2-TX;
        double dy=BY4_2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive4_2 = false;
        }
    }

    private void checkBHitT9_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX4-TX2;
        double dy=BY4-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive4 = false;
        }
    }

    private void checkBHitT10() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX5_2-TX;
        double dy=BY5_2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive = false;
            BIsLive5_2 = false;
        }
    }

    private void checkBHitT10_2() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=BX5-TX2;
        double dy=BY5-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + r)) {
            TIsLive2 = false;
            BIsLive5 = false;
        }
    }

    private void checkBHitT11_2() {
        if (!TIsLive) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=useBX2-TX;
        double dy=useBY2-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + useR/2)) {
            TIsLive = false;
            isUseBLive1 = false;
        }
    }

    private void checkBHitT11() {
        if (!TIsLive2) return;

        int tankRadius = Math.max(a, b)/2;
        double dx=useBX1-TX2;
        double dy=useBY1-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + useR/2)) {
            TIsLive2 = false;
            isUseBLive2 = false;
        }
    }

    private void createBuff(){
        Random random=new Random();
        int hhh=random.nextInt(10);
        if(hhh==0 || hhh==1){
            buff=2;
        }else{
            buff=1;
        }
        buff_X=random.nextInt(890)+30;
        buff_Y=random.nextInt(870)+30;
    }

    private void T1eatBuff(){
        int tankRadius = Math.max(a, b)/2;
        double dx=buff_X-TX;
        double dy=buff_Y-TY;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + buff_r/2)) {
            if(T1_type==0){
                T1_type=buff;
            }
            buff=0;
            buffTime=0;
        }
    }

    private void T2eatBuff(){
        int tankRadius = Math.max(a, b)/2;
        double dx=buff_X-TX2;
        double dy=buff_Y-TY2;
        double distance = Math.sqrt(dx*dx + dy*dy);

        if (distance < (tankRadius + buff_r/2)) {
            if(T2_type==0){
                T2_type=buff;
            }
            buff=0;
            buffTime=0;
        }
    }

    private void calculateLaserPath1() {

        int barrelLength = b / 2 + 15;
        laserX1 = TX - (int) (barrelLength * Math.cos(Math.toRadians(angle + 90)));
        laserY1 = TY - (int) (barrelLength * Math.sin(Math.toRadians(angle + 90)));

        double dirX = Math.cos(Math.toRadians(angle + 90));
        double dirY = Math.sin(Math.toRadians(angle + 90));
        laserEndX1 = (int) (laserX1-dirX * 2000);
        laserEndY1 = (int) (laserY1-dirY * 2000);
    }

    private void calculateLaserPath2() {

        int barrelLength = b / 2 + 15;
        laserX2 = TX2 - (int) (barrelLength * Math.cos(Math.toRadians(angle_2 + 90)));
        laserY2 = TY2 - (int) (barrelLength * Math.sin(Math.toRadians(angle_2 + 90)));

        double dirX = Math.cos(Math.toRadians(angle_2 + 90));
        double dirY = Math.sin(Math.toRadians(angle_2 + 90));
        laserEndX2 = (int) (laserX2-dirX * 2000);
        laserEndY2 = (int) (laserY2-dirY * 2000);
    }


    private void checkLaserCollision1() {
        if (!TIsLive2) return;

        Area laserArea = createLaserArea(laserX1, laserY1, laserEndX1, laserEndY1, a/2);
        Area tankArea = new Area(new Rectangle(TX2 - a/2, TY2 - b/2, a, b));

        if (laserArea.intersects(tankArea.getBounds2D())) {
            TIsLive2 = false;
        }
    }

    private void checkLaserCollision2() {
        if (!TIsLive) return;

        Area laserArea = createLaserArea(laserX2, laserY2, laserEndX2, laserEndY2, a/2);
        Area tankArea = new Area(new Rectangle(TX - a/2, TY - b/2, a, b));

        if (laserArea.intersects(tankArea.getBounds2D())) {
            TIsLive = false;
        }
    }

    private Area createLaserArea(int x1, int y1, int x2, int y2, int width) {
        Path2D path = new Path2D.Double();
        double angle = Math.atan2(y2-y1, x2-x1);
        double dx = width/2 * Math.sin(angle);
        double dy = width/2 * Math.cos(angle);

        path.moveTo(x1-dx, y1+dy);
        path.lineTo(x1+dx, y1-dy);
        path.lineTo(x2+dx, y2-dy);
        path.lineTo(x2-dx, y2+dy);
        path.closePath();

        return new Area(path);
    }


    @Override
    public void actionPerformed(ActionEvent e) {

    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(isUseBLive1){
            if(e.getKeyCode()==KeyEvent.VK_W){
                isUpB1=true;
            } else if (e.getKeyCode()==KeyEvent.VK_S) {
                isDowmB1=true;
            } else if (e.getKeyCode()==KeyEvent.VK_A) {
                isLeftB1=true;
            } else if (e.getKeyCode()==KeyEvent.VK_D) {
                isRightB1=true;
            }
        }else{
            if(e.getKeyCode()==KeyEvent.VK_W){
                vT=7;
                IsGo=true;
            } else if (e.getKeyCode()==KeyEvent.VK_S) {
                vT=-7;
                IsGo=true;
            } else if (e.getKeyCode()==KeyEvent.VK_A) {
                IsLeft=true;
            } else if (e.getKeyCode()==KeyEvent.VK_D) {
                IsRight = true;
            }
        }

        if(isUseBLive2){
            if(e.getKeyCode()==KeyEvent.VK_UP){
                isUpB2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                isDowmB2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                isLeftB2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                isRightB2=true;
            }
        }else{
            if(e.getKeyCode()==KeyEvent.VK_UP){
                vT2=7;
                IsGo2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                vT2=-7;
                IsGo2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                IsLeft2=true;
            } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                IsRight2=true;
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(isUseBLive1){
            if(e.getKeyCode()==KeyEvent.VK_W){
                isUpB1=false;
            } else if (e.getKeyCode()==KeyEvent.VK_S) {
                isDowmB1=false;
            } else if (e.getKeyCode()==KeyEvent.VK_A) {
                isLeftB1=false;
            } else if (e.getKeyCode()==KeyEvent.VK_D) {
                isRightB1=false;
            } else if (e.getKeyCode()==KeyEvent.VK_F) {
                isUseBLive1=false;
                useBX1=-20;
                isRightB1=false;
                isLeftB1=false;
                isUpB1=false;
                isDowmB1=false;
            }
        }else{
            if(e.getKeyCode()==KeyEvent.VK_W){
                IsGo=false;
            } else if (e.getKeyCode()==KeyEvent.VK_S) {
                IsGo=false;
            } else if (e.getKeyCode()==KeyEvent.VK_A) {
                IsLeft=false;
            } else if (e.getKeyCode()==KeyEvent.VK_D) {
                IsRight=false;
            } else if (e.getKeyCode()==KeyEvent.VK_F) {
                IsB=true;
            }
        }

        if(isUseBLive2){
            if(e.getKeyCode()==KeyEvent.VK_UP){
                isUpB2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                isDowmB2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                isLeftB2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                isRightB2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_M) {
                isUseBLive2=false;
                useBX2=-20;
                isRightB2=false;
                isLeftB2=false;
                isUpB2=false;
                isDowmB2=false;
            }
        }else{
            if(e.getKeyCode()==KeyEvent.VK_UP){
                IsGo2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_DOWN) {
                IsGo2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_LEFT) {
                IsLeft2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_RIGHT) {
                IsRight2=false;
            } else if (e.getKeyCode()==KeyEvent.VK_M) {
                IsB2=true;
            }
        }
    }
}

